package com.ivncorp.evnproject.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ivncorp.evnproject.R;
import com.ivncorp.evnproject.helpers.Utils;
import com.ivncorp.evnproject.model.ObjectCot;

public class ImageGalleryCotAdapter extends BaseAdapter {

	ArrayList<ObjectCot> imageUrls;
	Activity activity;

	public ImageGalleryCotAdapter(Activity activity,
			ArrayList<ObjectCot> _imageUrls) {
		this.imageUrls = _imageUrls;
		this.activity = activity;

	}

	public void clearData() {
		this.imageUrls.clear();
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return imageUrls.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	static class ViewHoler {
		ImageView checked;
		ImageView imageView;
		TextView txtName;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHoler viewHoler;

		if (convertView == null) {
			convertView = activity.getLayoutInflater().inflate(
					R.layout.item_gallery, parent, false);
			viewHoler = new ViewHoler();
			viewHoler.checked = (ImageView) convertView
					.findViewById(R.id.checked);
			viewHoler.imageView = (ImageView) convertView
					.findViewById(R.id.imageViewGallery);
			viewHoler.txtName = (TextView) convertView
					.findViewById(R.id.txtGalleryName);
			convertView.setTag(viewHoler);
		} else {
			viewHoler = (ViewHoler) convertView.getTag();
		}

		String name = imageUrls.get(position).getName();
		if (!name.isEmpty()) {
			viewHoler.txtName.setText(name);
		}
		String uri = imageUrls.get(position).getImage_1();
		if (uri != null) {
			viewHoler.imageView.setImageBitmap(Utils.decodeFile(uri, 100, 100));
		}

		if (imageUrls.get(position).getUploaded() == 0) {
			viewHoler.checked.setVisibility(View.GONE);
		} else {
			viewHoler.checked.setVisibility(View.VISIBLE);
		}
		return convertView;
	}

}